package com.cltech.controller.sys;

import com.cltech.dto.SysDicItemDto;
import com.cltech.entity.sys.SysDicItem;
import com.cltech.frame.result.PageResult;
import com.cltech.frame.result.ResponseBean;
import com.cltech.service.sys.SysDicItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by Administrator on 2019/4/7.
 */
@RestController
@RequestMapping("api/sysDicItem")
@Api(tags = { "数据字典项" })
public class SysDicItemController {
    @Autowired
    private SysDicItemService sysDicItemService;
    @PostMapping
    @ApiOperation("新增或保存字典项")
    public Object addOrUpdate(@RequestBody @Valid SysDicItemDto sysDicItemDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseBean.fail(bindingResult.getFieldError().getDefaultMessage());
        } else {
           return sysDicItemService.addOrUpdate(sysDicItemDto);
        }
    }
    @GetMapping
    @ApiOperation("分页查询字典项")
    public Object list(HttpServletRequest request, @RequestParam(value="page",defaultValue="0")int page,
                       @RequestParam(value="size",defaultValue="10")int size) {
        return ResponseBean.success(PageResult.result(sysDicItemService.findAll(request.getParameterMap(), page,size)));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询字典项")
    public Object get(@PathVariable String id) {
        return sysDicItemService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除字典项")
    public Object delete(@PathVariable String id) {
        sysDicItemService.deleteById(id);
        return ResponseBean.success("删除成功");
    }
}
